/*
 * Copyright (C) 2015 andrea
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package meteocal.boundary;

import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import meteocal.control.WeatherControl;
import meteocal.entity.Event;
import meteocal.entity.Users;
import meteocal.entityManager.EventManager;
import meteocal.entityManager.UserManager;
import meteocal.exception.CityNotFoundException;
import meteocal.exception.UnavailableForecastException;
import meteocal.weather.Weather;

/**
 *
 * @author andrea
 */
@Named
@ViewScoped
public class EventBean implements Serializable{
    
    @Inject
    UserManager um;
    
    @Inject
    EventManager em;
    
    @Inject
    WeatherControl wc;
    
    private Event event;
    
    private List<Users> invitedUsers;
    
    private Users selectedUser;
    
    private boolean okFlag;
    
    @PostConstruct
    public void init(){
        okFlag = false;
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance()
        .getExternalContext().getRequest();
        String ev = req.getParameter("event");
        
        
        if(ev == null || ev.equals("")){
            FacesMessage message = new FacesMessage(FacesMessage.FACES_MESSAGES, "Error: Invalid Event");
            addMessage(message);
            return;
        }
        
        Long idEvent;
        
        try{
            idEvent = Long.parseLong(ev);
            
        }catch(Exception e){
            
            FacesMessage message = new FacesMessage(FacesMessage.FACES_MESSAGES, "Error: Invalid Event");
            addMessage(message);
            return;
        }
        
        
        Event evento = em.findById(idEvent);
        if(evento!=null && um.canSeeEvent(evento) ){
            okFlag = true;
            this.event= evento;
            invitedUsers = em.getParticipants(event);
        }else{
            FacesMessage message = new FacesMessage(FacesMessage.FACES_MESSAGES, "Error: Invalid Event");
                addMessage(message);  
        }
    }

    /**
     * @return the event
     */
    public Event getEvent() {
        return event;
    }

    /**
     * @param event the event to set
     */
    public void setEvent(Event event) {
        this.event = event;
    }
    
    public String getWeather(){
        try {
            Weather forecast = wc.getEventForecast(event);
            
            String fore = forecast.toString();
            return fore;
        } catch (UnavailableForecastException ex) {
            return "Unavailable :(";
        } catch (CityNotFoundException ex) {
            return "City Not Found :(";
        }
    }

    /**
     * @return the invitedUsers
     */
    public List<Users> getUsers() {
        return invitedUsers;
    }

    /**
     * @return the selectedUser
     */
    public Users getSelectedUser() {
        return selectedUser;
    }

    /**
     * @param selectedUser the selectedUser to set
     */
    public void setSelectedUser(Users selectedUser) {
        this.selectedUser = selectedUser;
    }
    
    public String visitUser(){
        return "/user/UserPage?faces-redirect=true&amp;email="+selectedUser.getEmail();
    }
    
    
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    /**
     * @return the okFlag
     */
    public boolean isOkFlag() {
        return okFlag;
    }
}
